package com.scud.system.controller;

import com.scud.common.result.Result;
import com.scud.model.system.dto.LoginDto;
import com.scud.model.system.pojo.User;
import com.scud.system.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author vermouth
 */
@RestController
@RequestMapping("/api/system/index")
@Slf4j
public class IndexController {

    @Autowired
    private IndexService indexService;


    /**
     * 发送验证码功能
     *
     * @param phone:
     * @return Result
     * @author vermouth
     */
    @GetMapping("/code/{phone}")
    public Result sendCode(@PathVariable String phone) {
        log.info("发送验证码：{}", phone);
        String code = indexService.sendCode(phone);
        return Result.success(code);
    }

    /**
     * 用户登录
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDto loginDto) {
        log.info("用户登录：{}", loginDto);
        String jwt = indexService.login(loginDto);
        return Result.success(jwt);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/userinfo")
    public Result<User> getUserinfo(@RequestHeader(value = "token") String token) {
        log.info("获取用户信息：{}", token);
        User user = indexService.getUserinfo(token);
        return Result.success(user);
    }
}
